Skip to content

Marshal Windows DropTarget registration to window thread and throttle DragOver - #102

Merged
lijy91 merged 1 commit into
mainfrom
fix/windows-drop-target-issue-57
Nov 29, 2026
Merged

lijy91 merged 1 commit into
mainfrom
fix/windows-drop-target-issue-57

Conversation

@lijy91

@lijy91 lijy91 commented Nov 29, 2026

Copy link
Copy Markdown
Member

Summary

Fixes the Windows drag-source (explorer.exe) freeze and hover stutter reported in libnativeapi/nativeapi#57:

  1. Marshal OLE DropTarget registration/revocation onto the window's owning Win32 thread (src/platform/windows/drop_target_windows.cpp):
    • Under Flutter's UIThreadPolicy::RunOnSeparateThread, DropTarget::AddListener() runs on the Dart UI isolate thread, which has no Win32 message loop. Calling OleInitialize + RegisterDragDrop there placed DropTargetCom in an unpumped STA apartment, so OLE's cross-apartment DragEnter RPC from explorer.exe deadlocked forever.
    • Use detail::PostToThread (RunOnOwningThread) to run OleInitialize, DropTargetCom creation, RegisterDragDrop, and teardown (RevokeDragDrop / Release / OleUninitialize) on hwnd's owning Win32 thread, with an atomic alive guard.
    • If RegisterDragDrop returns DRAGDROP_E_ALREADYREGISTERED, call RevokeDragDrop and retry once.
  2. Gate and deduplicate IDropTargetHelper calls & throttle DragOver:
    • Track helper_active_ so DragOver / DragLeave / Drop only call IDropTargetHelper after a successful helper_->DragEnter(...); skip redundant helper_->DragOver calls when position and effect are unchanged.
    • Cache window DPI scale factor on DragEnter and throttle DropTargetMovedEvent emission to ~60 Hz (16 ms) while still returning the exact DROPEFFECT immediately on every DragOver.
  3. Add cross-thread regression test (tests/drop_target_ui_thread_windows_test.cpp):
    • Verifies registering DropTarget from a non-pumping isolate thread and invoking DragEnter / DragOver / DragLeave from a third STA thread completes without freezing, runs callbacks on the platform thread, and revokes cleanly on destruction.

Related to libnativeapi/nativeapi#57.

Verification

  • drop_target_ui_thread_windows_test: Passed on Windows (built cleanly on top of origin/main).
  • Verified with a Release Flutter RunOnSeparateThread app dragging files from Explorer: explorer.exe no longer freezes and hover/drop events update smoothly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant